package org.dataone.dataUsage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import org.apache.commons.io.IOUtils;
import org.dataone.client.D1RestClient;
import org.dataone.service.types.v1.NodeType;
import org.dataone.service.util.TypeMarshaller;
import org.json.JSONObject;

public class MNodeMetric {
	private String CN_URL;
	public String getCN_URL() {
		return CN_URL;
	}
	public void setCN_URL(String cN_URL) {
		CN_URL = cN_URL;
	}
	/**
	 * Returns an ArrayList of Member Node name.
	 */
	public ArrayList<String> getMNodeList() {
		String url = CN_URL + "/cn/v1/node/";
		D1RestClient MNclient = new D1RestClient();
		ArrayList<String> mnodelist = new ArrayList<String>();
		try {
			org.dataone.service.types.v1.NodeList list = TypeMarshaller
					.unmarshalTypeFromStream(
							org.dataone.service.types.v1.NodeList.class,
							MNclient.doGetRequest(url, true));
			for (int i = 0; i < list.sizeNodeList(); i++) {
				if (list.getNode(i).getType().equals(NodeType.MN)) {
					mnodelist
							.add("\""
									+ list.getNode(i).getIdentifier()
											.getValue() + "\"");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mnodelist;
	}
	/**
	 * This method is to obtain each Member Node's Download Metric by month.
	 * Returns an ArrayList of MonthlyMetric type.
	 * 
	 * @param MNode
	 *            the Member Node name
	 * @param timespan
	 *            the time span for metric display
	 **/
	public ArrayList<MonthlyMetric> getMNDownloadMetricByMonth(String MNode,
			int timespan) {
		ArrayList<MonthlyMetric> result = new ArrayList<MonthlyMetric>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar c1 = Calendar.getInstance();
		c1.add(Calendar.MONTH, -(timespan + 1));
		for (int i = 0; i < timespan; i++) {
			String metricDate = sdf.format(c1.getTime());
			String starttime = sdf.format(c1.getTime()) + "-01T00:00:00.0000Z";
			c1.add(Calendar.MONTH, 1);
			String endtime = sdf.format(c1.getTime()) + "-01T00:00:00.0000Z";
			try {
				MonthlyMetric temp = new MonthlyMetric();
				temp.setMetricValue(getMNJSON(
						MNode.subSequence(10, MNode.length() - 1).toString(),
						starttime, endtime));
				temp.setMetricDate(metricDate);
				result.add(temp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	/**
	 * This method is to get the exact data for a Member Node.
	 * 
	 * @param MNode
	 *            the Member Node name
	 * @param startDate
	 *            the data starts point
	 * @param endDate
	 *            the data ends point
	 */
	public String getMNJSON(String MNode, String startDate, String endDate) {

		D1RestClient MNclient = new D1RestClient();
		String url = CN_URL
				+ "/solr/d1-cn-log/select/?q=event:read+dateLogged:["
				+ startDate + "%20TO%20" + endDate + "]+nodeId:urn%5C:node%5C:"
				+ MNode + "&rows=0&wt=json";
		try {
			String responseBody = IOUtils.toString(
					MNclient.doGetRequest(url, true), "UTF-8");
			JSONObject resultObject = new JSONObject(responseBody);
			String result = resultObject.getJSONObject("response").getString(
					"numFound");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
